package com.hsytech.dijiebao.person.activity;

import java.util.ArrayList;
import java.util.List;

import com.hsytech.dijiebao.R;
import com.hsytech.dijiebao.base.BaseActivity;
import com.hsytech.dijiebao.bean.HomeHelpHotle;
import com.hsytech.dijiebao.bean.HomeHelpTaxi;
import com.hsytech.dijiebao.bean.HomeMyHotle;
import com.hsytech.dijiebao.bean.HomeMyTaxi;
import com.hsytech.dijiebao.person.fragment.ReleaseHotleFrament;
import com.hsytech.dijiebao.person.fragment.ReleaseTaxiFrament;
import com.hsytech.dijiebao.view.PagerSlidingTabStrip;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class ReleaseActivity extends BaseActivity {
	private View root;
	private ViewPager activity_repair_viewpager;
	private ReleaseHotleFrament hotleFrament;
	private ReleaseTaxiFrament taxiFrament;
	private List<Fragment> myFragmentList;
	private ImageButton top_back_btn;
	private Button bar_right_top_btn;
	private TextView bar_title_tv;
	private final Handler handler = new Handler();
	private ImageView new_city00;
	private PagerSlidingTabStrip tabs;
	private MyReleasePagerAdapter adapter;
	private DisplayMetrics dm;
	private Drawable oldBackground = null;
	private int currentColor = 0xff66ccff;
	private int afterColor = 0xff66ccff;
	private List<HomeMyHotle> hm;
	private List<HomeHelpHotle> hh;
	private List<HomeHelpTaxi> hh1;
	private List<HomeMyTaxi> hm1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		dm = getResources().getDisplayMetrics();
		setContentView(R.layout.activity_release_main);
		initTitle();
		initBody();
		// getHttp();

	}

	private void initTitle() {
		bar_right_top_btn = (Button) findViewById(R.id.bar_right_top_btn);
		bar_right_top_btn.setVisibility(View.GONE);

		top_back_btn = (ImageButton) findViewById(R.id.top_back_btn);
		top_back_btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		// bar_right_top_btn = (Button)
		// root.findViewById(R.id.bar_right_top_btn);
		// bar_right_top_btn.setVisibility(View.GONE);
		bar_title_tv = (TextView) findViewById(R.id.bar_title_tv);
		bar_title_tv.setText("�ҵķ���");
	}

	// private void getHttp() {
	// // ����
	// RequestParams data = new RequestParams();
	// data.put("token", SharedPreferencesTools.getTOKEN(this));
	// data.put("type", 2);
	// HttpUtils.get(Constants.HTTP_MY_LIST, this, data, new
	// JsonResponseHandler() {
	// @Override
	// public void onSuccess(JSONObject data) {
	// // JSONObject response = data.optJSONObject("result");
	// hm = com.alibaba.fastjson.JSONArray
	// .parseArray(data.optJSONObject("result").optJSONArray("rental").toString(),
	// HomeMyHotle.class);
	// hh = com.alibaba.fastjson.JSONArray
	// .parseArray(data.optJSONObject("result").optJSONArray("find").toString(),
	// HomeHelpHotle.class);
	//
	// hm1 = com.alibaba.fastjson.JSONArray
	// .parseArray(data.optJSONObject("result").optJSONArray("rental").toString(),
	// HomeMyTaxi.class);
	//
	// hh1 = com.alibaba.fastjson.JSONArray
	// .parseArray(data.optJSONObject("result").optJSONArray("find").toString(),
	// HomeHelpTaxi.class);
	// initBody();
	// }
	// });
	// }

	private void setTabsValue() {

		// ����Tab���Զ��������Ļ��
		tabs.setShouldExpand(true);
		// ����Tab�ķָ�����͸����
		tabs.setDividerColor(Color.TRANSPARENT);
		// ����Tab�ײ��ߵĸ߶�
		tabs.setUnderlineHeight((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, dm));
		// ����Tab Indicator�ĸ߶�
		tabs.setIndicatorHeight((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, dm));
		// ����Tab�������ֵĴ�С
		tabs.setTextSize((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, dm));
		// ����Tab Indicator����ɫ
		tabs.setIndicatorColor(currentColor);
		// ����ѡ��Tab���ֵ���ɫ (�������Զ����һ������)
		tabs.setSelectedTextColor(afterColor);
		// ȡ�����Tabʱ�ı���ɫ
		tabs.setTabBackground(0);

		tabs.setTextColor(getResources().getColor(R.color.gary_first));

	}

	private void initBody() {
		myFragmentList = new ArrayList<Fragment>();
		hotleFrament = new ReleaseHotleFrament();
		taxiFrament = new ReleaseTaxiFrament();

		myFragmentList.add(hotleFrament);
		myFragmentList.add(taxiFrament);
		activity_repair_viewpager = (ViewPager) findViewById(R.id.activity_repair_viewpager);
		activity_repair_viewpager.setCurrentItem(0);
		tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
		setTabsValue();
		adapter = new MyReleasePagerAdapter(getSupportFragmentManager(), myFragmentList);

		activity_repair_viewpager.setAdapter(adapter);

		final int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4,
				getResources().getDisplayMetrics());
		activity_repair_viewpager.setPageMargin(pageMargin);

		tabs.setViewPager(activity_repair_viewpager);
	}

	public static class MyReleasePagerAdapter extends FragmentPagerAdapter {

		private final String[] TITLES = { "�Ƶ�", "�⳵" };
		private List<Fragment> myFragmentsList;
		private FragmentManager myFragmentManager;

		public void setFragments() {
			if (myFragmentsList != null) {
				FragmentTransaction ft = myFragmentManager.beginTransaction();
				for (Fragment f : myFragmentsList) {
					ft.remove(f);
				}
				ft.commit();
				// ft = null;
				myFragmentManager.executePendingTransactions();
			}
			// notifyDataSetChanged();
		}

		public MyReleasePagerAdapter(FragmentManager fragmentmanager, List<Fragment> list) {
			super(fragmentmanager);
			myFragmentsList = list;
		}

		public int getItemPosition(Object obj) {
			return super.getItemPosition(obj);
		}

		public MyReleasePagerAdapter(FragmentManager fm) {
			super(fm);
		}

		public int getCount() {
			return myFragmentsList.size();
		}

		public Fragment getItem(int i) {
			return (Fragment) myFragmentsList.get(i);
		}

		@Override
		public CharSequence getPageTitle(int position) {
			return TITLES[position];
		}

	}

	// @Override
	// protected void onActivityResult(int requestCode, int resultCode, Intent
	// data) {
	// // TODO Auto-generated method stub
	// Log.v("mylog", "ReleaseActivity onActivityResult");
	// getHttp();
	// super.onActivityResult(requestCode, resultCode, data);
	// }

	/*
	 * @Override protected void onPause() { // TODO Auto-generated method stub
	 * super.onPause(); if (adapter != null) {
	 * 
	 * adapter.setFragments(); Log.d("mylog", "adapteradapter"); } }
	 * 
	 * @Override protected void onResume() { super.onResume();
	 * 
	 * getHttp(); Log.d("mylog", "onResume"); }
	 * 
	 * @Override protected void onResumeFragments() { super.onResumeFragments();
	 * Log.d("mylog", "onResumeFragments"); }
	 */
	// @Override
	// protected void onActivityResult(int requestCode, int resultCode, Intent
	// data) {
	// // TODO Auto-generated method stub
	// super.onActivityResult(requestCode, resultCode, data);
	// Log.d("mylog", requestCode+"requestCode");
	// if (requestCode == 75536) {
	// // initBody();
	// // getUI();
	// Log.d("mylog", "1000010000");
	// getHttp();
	// }
	// if (requestCode == 20000) {
	// // initBody();
	// // getUI();
	// getHttp();
	// }
	//
	// }

}
